package com.sanjiahantang.youjiaclient.module.city.views.impl;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.amap.api.location.AMapLocationClient;
import com.sanjiahantang.uilibrary.views.SideBarView;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpActivity;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.module.city.beans.CityBean;
import com.sanjiahantang.youjiaclient.module.city.presenters.CityPresenter;
import com.sanjiahantang.youjiaclient.module.city.views.CityView;

import java.util.List;


/**
 * Created by zy on 2017/9/19.
 */

public class CityActivity extends BaseMvpActivity<CityView,CityPresenter> implements CityView, View.OnClickListener, CityView.OnLoadListener {

    private View back,empty_view;
    private ListView lvCity,lvSearchResult;
    private SideBarView sbLetter;
    private TextView txtOverLay;
    private  List<CityBean.City> cities;
    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public CityPresenter initPresenter() {
        return new CityPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_city);
        back = findViewById(R.id.fl_Left);
        TextView title = (TextView) findViewById(R.id.title);
        title.setText("选择城市");
        back.setOnClickListener(this);

        lvCity = (ListView) findViewById(R.id.lvCity);
        lvSearchResult = (ListView) findViewById(R.id.lvSearchResult);
        sbLetter = (SideBarView) findViewById(R.id.sbLetter);
        txtOverLay = (TextView) findViewById(R.id.txtOverLay);
    }

    @Override
    public void initData() {
        sbLetter.setOnLetterSelectListen(new SideBarView.LetterSelectListener() {
            @Override
            public void onLetterSelected(String letter) {
                int position = presenter.getLetterPosition(letter);
                if (position > -1){
                    lvCity.setSelection(position);
                }

                txtOverLay.setVisibility(View.VISIBLE);
                txtOverLay.setText(letter);
            }

            @Override
            public void onLetterChanged(String letter) {
                int position = presenter.getLetterPosition(letter);
                if (position > -1){
                    lvCity.setSelection(position);
                }
                txtOverLay.setVisibility(View.VISIBLE);
                txtOverLay.setText(letter);
            }

            @Override
            public void onLetterReleased(String letter) {
                txtOverLay.setVisibility(View.GONE);
            }
        });
        presenter.loadCityList($this,this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fl_Left:
                finish();
                break;
        }
    }


    @Override
    public void initCityList(BaseAdapter adapter) {
        lvCity.setAdapter(adapter);
    }

    @Override
    public void citySelected(CityBean.City city) {
        ConfigValue.city = city.getRegion_name();
        setResult(RESULT_OK,new Intent().putExtra(TAG_CITY,city));
        finish();
    }

    @Override
    public AMapLocationClient getAMapLocationClient() {
        return new AMapLocationClient($this);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        presenter.onRequestPermissionsResult($this,requestCode, permissions, grantResults);
    }
}
